package cn.gson.crmdemo.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;

import cn.gson.crmdemo.model.pojo.Emp;
import cn.gson.crmdemo.model.pojo.FunctionType;
import cn.gson.crmdemo.model.service.ContractService;
import cn.gson.crmdemo.model.service.LoginService;
import cn.gson.crmdemo.model.service.PrivilegeService;

@RestController
/*@Controller*/
public class MainController {
	@Autowired
	ContractService cs;
	@Autowired
	LoginService ls;
	@Autowired
	PrivilegeService ps;
	
	
	
	/**
	 * 登录
	 */
	@RequestMapping("login")
	public String login(String root,String pass,HttpServletRequest request) {
		HttpSession session = request.getSession();
		Emp emp = ls.login(root, pass);
		if(emp != null) {
			session.setAttribute("emp", emp);
			return "success";
		}
		return "fail";
	}
	
	
	
	
	/**
	 * 注销
	 */
	@RequestMapping("emp-cancellation")
	public String cancellation(HttpServletRequest request) {
		Emp emp = (Emp) request.getSession().getAttribute("emp");
		ls.cancellation(emp.getEmpId());
		request.getSession().removeAttribute("emp");
		return "fail";
	}
	/**
	 * 查询某员工所有权限
	 */
	@RequestMapping("emp-funcionType")
	@ResponseBody
	public List<FunctionType> selectFuntypeAll(HttpServletRequest request){
		Emp emp = (Emp) request.getSession().getAttribute("emp");
		return ps.selectFuntypeAll(emp.getEmpId());
	}
	/**
	 * 查询重要通知
	 */
	@RequestMapping("notice-All0")
	@ResponseBody
	public JSONArray selectNoticeAll0() {
		return ps.selectNoticeAll0();
	}
	/**
	 * 查询通知
	 */
	@RequestMapping("notice-All1")
	@ResponseBody
	public JSONArray selectNoticeAll1() {
		return ps.selectNoticeAll1();
	}
	/**
	 * 查询重要业界动态
	 */
	@RequestMapping("notice-All2")
	@ResponseBody
	public JSONArray selectNoticeAll2() {
		return ps.selectNoticeAll2();
	}
	/**
	 * 查询其他
	 */
	@RequestMapping("notice-All3")
	@ResponseBody
	public JSONArray selectNoticeAll3() {
		return ps.selectNoticeAll3();
	}
	/**
	 * 查询某员工最近十篇日志
	 */
	@RequestMapping("emp-journal")
	@ResponseBody
	public JSONArray selectJournal(HttpServletRequest request) {
		Emp emp = (Emp) request.getSession().getAttribute("emp");
		return ps.selectJournal(emp.getEmpId());
	}
}
